package com.ssy.lingxi.order.serviceimpl.feign;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.service.feign.IReportFeignService;
import com.ssy.lingxi.order.utils.OrderStringUtil;
import com.ssy.lingxi.report.api.constant.ReportConstants;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.feign.ReportControllerFeign;
import com.ssy.lingxi.report.api.vo.request.AddOrderVO;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeAddVO;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 报表服务Feign接口调用
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-23
 */
@Service
public class ReportFeignServiceImpl implements IReportFeignService {
    private static final Logger logger = LoggerFactory.getLogger(ReportFeignServiceImpl.class);

    @Resource
    private ReportControllerFeign reportControllerFeign;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * “提交订单”后，通知供应商会员有新订单
     * @param buyerMemberId  采购商会员id
     * @param buyerRoleId    采购商角色id
     * @param vendorMemberId 供应商会员Id
     * @param vendorRoleId   供应商会员角色Id
     * @param amount         订单金额
     */
    @Async
    @Override
    public void reportVendorOrderAmountAsync(Long buyerMemberId, Long buyerRoleId, Long vendorMemberId, Long vendorRoleId, Double amount) {
        AddOrderVO orderVO = new AddOrderVO();
        orderVO.setBuyerMemberId(buyerMemberId);
        orderVO.setBuyerRoleId(buyerRoleId);
        orderVO.setMemberId(vendorMemberId);
        orderVO.setRoleId(vendorRoleId);
        orderVO.setAmount(amount);
        Wrapper<?> result = reportControllerFeign.addOrder(orderVO);
        if(result.getCode() != ResponseCode.SUCCESS.getCode()) {
            logger.error("调用报表服务错误：" + result.getMessage());
        }
    }

    /**
     * 发送采购商订单统计数量
     *
     * @param buyerMemberId       采购会员Id
     * @param buyerRoleId         采购会员角色Id
     * @param decreaseBuyerOperateCode 要减少统计的操作
     * @param increaseBuyerOperateCode 要增加统计的操作
     */
    @Async
    @Override
    public void reportBuyerOrderAsync(Long buyerMemberId, Long buyerRoleId, Integer decreaseBuyerOperateCode, Integer increaseBuyerOperateCode) {
        reportBuyerOrderAsync(buyerMemberId, buyerRoleId, decreaseBuyerOperateCode, 1L, increaseBuyerOperateCode, 1L);
    }

    /**
     * 发送采购商订单统计数量
     *
     * @param buyerMemberId            采购会员Id
     * @param buyerRoleId              采购会员角色Id
     * @param decreaseBuyerOperateCode 要减少统计的操作
     * @param decreaseCount            要减少的数量
     * @param increaseBuyerOperateCode 要增加统计的操作
     * @param increseCount             要增加的数量
     */
    @Async
    @Override
    public void reportBuyerOrderAsync(Long buyerMemberId, Long buyerRoleId, Integer decreaseBuyerOperateCode, Long decreaseCount, Integer increaseBuyerOperateCode, Long increseCount) {

        OperateNoticeVO noticeVO = new OperateNoticeVO();
        noticeVO.setMemberId(buyerMemberId);
        noticeVO.setRoleId(buyerRoleId);
        noticeVO.setDataSource(OperateDataSourceEnum.TRADE_PURCHASE_ORDER.getCode());
        noticeVO.setIncreaseOperateType(increaseBuyerOperateCode);
        noticeVO.setIncreaseCount(increaseBuyerOperateCode.equals(0) ? 0L : increseCount);
        noticeVO.setReduceOperateType(decreaseBuyerOperateCode);
        noticeVO.setReduceCount(decreaseBuyerOperateCode.equals(0) ? 0L : decreaseCount);

        OperateNoticeAddVO addVO = new OperateNoticeAddVO();
        addVO.setOperateNoticeVO(noticeVO);
        addVO.setOperateNoticeVOList(null);
        try {
            rabbitTemplate.convertAndSend(ReportConstants.REPORT_SYS_OPERATE_NOTICE_EXCHANGE, ReportConstants.REPORT_SYS_OPERATE_NOTICE_ROUTINGKEY, OrderStringUtil.serializeObject(addVO));
        } catch (Exception e) {
            logger.error(String.format("发送采购商订单统计：buyerMemberId: %d， buyerRoleId: %d, decreaseBuyerOperateCode: %d, decreaseCount: %d, increaseBuyerOperateCode：%d, increaseCount: %d, 错误：%s", buyerMemberId, buyerRoleId, decreaseBuyerOperateCode, decreaseCount, increaseBuyerOperateCode, increseCount, e.getMessage()));
        }
    }

    /**
     * 发送供应商订单统计数据
     *
     * @param vendorMemberId      供应会员Id
     * @param vendorRoleId        供应会员角色Id
     * @param decreaseVendorOperateCode 要减少统计的操作
     * @param increaseVendorOperateCode 要增加统计的操作
     */
    @Override
    public void reportVendorOrderAsync(Long vendorMemberId, Long vendorRoleId, Integer decreaseVendorOperateCode, Integer increaseVendorOperateCode) {
        reportVendorOrderAsync(vendorMemberId, vendorRoleId, decreaseVendorOperateCode, 1L, increaseVendorOperateCode, 1L);
    }

    /**
     * 发送供应商订单统计数据
     *
     * @param vendorMemberId            供应会员Id
     * @param vendorRoleId              供应会员角色Id
     * @param decreaseVendorOperateCode 要减少统计的操作
     * @param decreaseCount             要减少的数量
     * @param increaseVendorOperateCode 要增加统计的操作
     * @param increaseCount             要增加的数量
     */
    @Async
    @Override
    public void reportVendorOrderAsync(Long vendorMemberId, Long vendorRoleId, Integer decreaseVendorOperateCode, Long decreaseCount, Integer increaseVendorOperateCode, Long increaseCount) {
        OperateNoticeVO noticeVO = new OperateNoticeVO();
        noticeVO.setMemberId(vendorMemberId);
        noticeVO.setRoleId(vendorRoleId);
        noticeVO.setDataSource(OperateDataSourceEnum.TRADE_SALE_ORDER.getCode());
        noticeVO.setIncreaseOperateType(increaseVendorOperateCode);
        noticeVO.setIncreaseCount(increaseVendorOperateCode.equals(0) ? 0L : increaseCount);
        noticeVO.setReduceOperateType(decreaseVendorOperateCode);
        noticeVO.setReduceCount(decreaseVendorOperateCode.equals(0) ? 0L : decreaseCount);

        OperateNoticeAddVO addVO = new OperateNoticeAddVO();
        addVO.setOperateNoticeVO(noticeVO);
        addVO.setOperateNoticeVOList(null);
        try {
            rabbitTemplate.convertAndSend(ReportConstants.REPORT_SYS_OPERATE_NOTICE_EXCHANGE, ReportConstants.REPORT_SYS_OPERATE_NOTICE_ROUTINGKEY, OrderStringUtil.serializeObject(addVO));
        } catch (Exception e) {
            logger.error(String.format("发送供应商订单统计：vendorMemberId: %d， vendorRoleId: %d, decreaseVendorOperateCode: %d, decreaseCount: %d, increaseVendorOperateCode: %d, increaseCount: %d, 错误：%s", vendorMemberId, vendorRoleId, decreaseVendorOperateCode, decreaseCount, increaseVendorOperateCode, increaseCount, e.getMessage()));
        }
    }
}
